using System;
using System.Collections.Generic;
using System.Text;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;
namespace THSERP
{
    public class ProductAttributeList : THSTreeList
    {
        public ProductAttributeList()
        {

        }
        //hieu.nguyen remove 10/08/2011 reason excute 2 times key up on gridcontrol
        //protected override void TreeList_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        //{
            
        //}

        public override void InvalidateTreeList(int iObjectID)
        {
            InvalidateTreeList(iObjectID, false);
        }

        public override void InvalidateTreeList(int iObjectID, bool checkAll)
        {
            ICProductsInfo objProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(iObjectID);
            if (objProductsInfo != null)
            {
                ICDepartmentsInfo objDepartmentsInfo = (ICDepartmentsInfo)new ICDepartmentsController().GetObjectByID(objProductsInfo.FK_ICDepartmentID);
                ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)new ICProductGroupsController().GetObjectByID(objProductsInfo.FK_ICProductGroupID);
                this.Clear();
                if (objDepartmentsInfo != null && objProductGroupsInfo != null)
                {
                    base.InvalidateTreeList(iObjectID, checkAll);

                    SetProductAttributeValues(objProductsInfo, String.Empty, objProductsInfo.ICProductName + objDepartmentsInfo.ICDepartmentNo + objProductGroupsInfo.ICProductGroupNo);

                    CalculateInventory(THSApp.CurrentCompanyInfo.FK_BRBranchID);
                }
                if (TreeListControl != null)
                    TreeListControl.RefreshDataSource();
            }
        }

        public void InvalidateTreeList(int productID, int stockID, bool checkAll)
        {
            ICProductsInfo objProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(productID);
            if (objProductsInfo != null)
            {
                ICDepartmentsInfo objDepartmentsInfo = (ICDepartmentsInfo)new ICDepartmentsController().GetObjectByID(objProductsInfo.FK_ICDepartmentID);
                ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)new ICProductGroupsController().GetObjectByID(objProductsInfo.FK_ICProductGroupID);
                this.Clear();
                if (objDepartmentsInfo != null & objProductGroupsInfo != null)
                {
                    base.InvalidateTreeList(productID, checkAll);

                    SetProductAttributeValues(objProductsInfo, String.Empty, objProductsInfo.ICProductName + objDepartmentsInfo.ICDepartmentNo + objProductGroupsInfo.ICProductGroupNo);

                    CalculateStockQuantity(stockID);
                    CalculateStockSOQuantity(stockID);
                    CalculateStockPOQuantity(stockID);
                }
                if (TreeListControl != null)
                    TreeListControl.RefreshDataSource();
            }
        }

        public void CalculateInventory(int branchID)
        {
            ResetQuantity();

            ICStocksController objStocksController = new ICStocksController();
            ICStocksInfo objDefaultStocksInfo = objStocksController.GetBranchStock(branchID);
            if (objDefaultStocksInfo == null)
                objDefaultStocksInfo = objStocksController.GetStockCentral(branchID);
            if (objDefaultStocksInfo != null)
            {
                CalculateStockQuantity(objDefaultStocksInfo.ICStockID);
                CalculateStockSOQuantity(objDefaultStocksInfo.ICStockID);
                CalculateStockPOQuantity(objDefaultStocksInfo.ICStockID);
            }

            ICStocksInfo objTransitStocksInfo = objStocksController.GetTransitStock(branchID);
            if (objTransitStocksInfo != null)
                CalculateStockTransitQuantity(objTransitStocksInfo.ICStockID);

            ICStocksInfo objMaintainanceStocksInfo = objStocksController.GetMaintainanceStock(branchID);
            if (objMaintainanceStocksInfo != null)
                CalculateStockMaintainanceQuantity(objMaintainanceStocksInfo.ICStockID);

            ICStocksInfo objRedemptionStocksInfo = objStocksController.GetRedemptionStock(branchID);
            if (objRedemptionStocksInfo != null)
                CalculateStockRedemptionQuantity(objRedemptionStocksInfo.ICStockID);
        }

        private void ResetQuantity()
        {
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                objProductAttributesInfo.ICInventoryStockQuantity = 0;
                objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = 0;
                objProductAttributesInfo.ICInventoryStockAvailableQuantity = 0;
                objProductAttributesInfo.ICInventoryStockPOQuantity = 0;
                objProductAttributesInfo.ICInventoryStockTransitQuantity = 0;
                objProductAttributesInfo.ICInventoryStockMaintainanceQuantity = 0;
                objProductAttributesInfo.ICInventoryStockRedemptionQuantity = 0;
                if (obj.HasChildren())
                    (obj.SubList as ProductAttributeList).ResetQuantity();
            }
        }

        public void SetProductAttributeValues(int productID)
        {
             ICProductsInfo objProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(productID);
             if (objProductsInfo != null)
             {
                 ICDepartmentsInfo objDepartmentsInfo = (ICDepartmentsInfo)new ICDepartmentsController().GetObjectByID(objProductsInfo.FK_ICDepartmentID);
                 ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)new ICProductGroupsController().GetObjectByID(objProductsInfo.FK_ICProductGroupID);
                 if (objDepartmentsInfo != null && objProductGroupsInfo != null)
                     SetProductAttributeValues(objProductsInfo, String.Empty, objProductsInfo.ICProductName + objDepartmentsInfo.ICDepartmentNo + objProductGroupsInfo.ICProductGroupNo);
             }
        }

        public void SetProductAttributeValues(ICProductsInfo objProductsInfo, String value, String serialNo)
        {
            for (int i = 0; i < this.Count; i++)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)this[i];
                ICDepartmentAttributesController objDepartmentAttributesController = new ICDepartmentAttributesController();
                ICDepartmentAttributeValuesController objDepartmentAttributeValuesController = new ICDepartmentAttributeValuesController();
                ICDepartmentAttributesInfo objDepartmentAttributesInfo = (ICDepartmentAttributesInfo)objDepartmentAttributesController.GetObjectByID(objProductAttributesInfo.FK_ICDepartmentAttributeID);
                ICDepartmentAttributeValuesInfo objDepartmentAttributeValuesInfo = (ICDepartmentAttributeValuesInfo)objDepartmentAttributeValuesController.GetObjectByID(objProductAttributesInfo.FK_ICDepartmentAttributeValueID);
                if (objDepartmentAttributesInfo != null && objDepartmentAttributeValuesInfo != null)
                {
                    if (this[i].HasChildren())
                    {
                        String seperator = String.Empty;
                        if (!String.IsNullOrEmpty(value))
                            seperator = " | ";
                        objProductAttributesInfo.ICProductAttributeValue = String.Format("{0}{1}{2}: {3}", value, seperator, objDepartmentAttributesInfo.ICDepartmentAttributeName, objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueValue);
                        (objProductAttributesInfo.SubList as ProductAttributeList).SetProductAttributeValues(objProductsInfo, objProductAttributesInfo.ICProductAttributeValue, serialNo + objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueNo);
                    }
                    else
                    {
                        String seperator = String.Empty;
                        if (!String.IsNullOrEmpty(value))
                            seperator = " | ";
                        objProductAttributesInfo.FK_ICDepartmentID = objProductsInfo.FK_ICDepartmentID;
                        objProductAttributesInfo.FK_ICProductGroupID = objProductsInfo.FK_ICProductGroupID;
                        objProductAttributesInfo.ICProductAttributeSerialNo = serialNo + objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueNo;
                        objProductAttributesInfo.ICProductAttributeValue = String.Format("{0}{1}{2}: {3}", value, seperator, objDepartmentAttributesInfo.ICDepartmentAttributeName, objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueValue);
                    }
                }
            }
        }

        #region Functions of calculating inventory
        public double CalculateStockQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockQuantity(stockID);
                    quantity += objProductAttributesInfo.ICInventoryStockQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockQuantity = objProductAttributesController.CalculateStockQuantity(stockID, objProductAttributesInfo);
                    quantity += objProductAttributesInfo.ICInventoryStockQuantity;
                }
            }
            return quantity;
        }

        public double CalculateStockSOQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockSOQuantity(stockID);
                    objProductAttributesInfo.ICInventoryStockAvailableQuantity = objProductAttributesInfo.ICInventoryStockQuantity - objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                    quantity += objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = objProductAttributesController.CalculateStockSOQuantity(stockID, objProductAttributesInfo);
                    objProductAttributesInfo.ICInventoryStockAvailableQuantity = objProductAttributesInfo.ICInventoryStockQuantity - objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                    quantity += objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                }
            }
            return quantity;
        }

        public double CalculateStockPOQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockPOQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockPOQuantity(stockID);
                    quantity += objProductAttributesInfo.ICInventoryStockPOQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockPOQuantity = objProductAttributesController.CalculateStockPOQuantity(stockID, objProductAttributesInfo);
                    quantity += objProductAttributesInfo.ICInventoryStockPOQuantity;
                }
            }
            return quantity;
        }

        public double CalculateStockTransitQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockTransitQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockTransitQuantity(stockID);
                    quantity += objProductAttributesInfo.ICInventoryStockTransitQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockTransitQuantity = objProductAttributesController.CalculateStockQuantity(stockID, objProductAttributesInfo);
                    quantity += objProductAttributesInfo.ICInventoryStockTransitQuantity;
                }
            }
            return quantity;
        }

        public double CalculateStockMaintainanceQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockMaintainanceQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockMaintainanceQuantity(stockID);
                    quantity += objProductAttributesInfo.ICInventoryStockMaintainanceQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockMaintainanceQuantity = objProductAttributesController.CalculateStockQuantity(stockID, objProductAttributesInfo);
                    quantity += objProductAttributesInfo.ICInventoryStockMaintainanceQuantity;
                }
            }
            return quantity;
        }

        public double CalculateStockRedemptionQuantity(int stockID)
        {
            double quantity = 0;
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                if (objProductAttributesInfo.HasChildren())
                {
                    objProductAttributesInfo.ICInventoryStockRedemptionQuantity = ((ProductAttributeList)objProductAttributesInfo.SubList).CalculateStockRedemptionQuantity(stockID);
                    quantity += objProductAttributesInfo.ICInventoryStockRedemptionQuantity;
                }
                else
                {
                    ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                    objProductAttributesInfo.ICInventoryStockRedemptionQuantity = objProductAttributesController.CalculateStockQuantity(stockID, objProductAttributesInfo);
                    quantity += objProductAttributesInfo.ICInventoryStockRedemptionQuantity;
                }
            }
            return quantity;
        }
        #endregion

        public override void SaveItemObjects()
        {
            for (int i = 0; i < this.Count; i++)
            {
                THSTreeListObject obj = this[i];
                //If object has no children and is not selected, remove it from list
                if (obj.HasChildren() == false)
                {
                    if (obj.Selected == false)
                    {
                        this.RemoveAt(i);
                        i--;
                    }
                }
                //If object is parent and does not have selected children, remove it from list
                else
                {
                    if (!obj.HasSelectedChildren())
                    {
                        this.RemoveAt(i);
                        i--;
                    }
                }
            }

            foreach (THSTreeListObject obj in this)
                (obj as ICProductAttributesInfo).FK_ICProductID = (Entity.MainObject as ICProductsInfo).ICProductID;
            base.SaveItemObjects();
        }

        public override void Duplicate()
        {
            foreach (THSTreeListObject obj in this)
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                objProductAttributesInfo.ICProductAttributeSerialNo = String.Empty;
                objProductAttributesInfo.ICInventoryStockQuantity = 0;
                objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = 0;
                objProductAttributesInfo.ICInventoryStockAvailableQuantity = 0;
                objProductAttributesInfo.ICInventoryStockPOQuantity = 0;
                objProductAttributesInfo.ICInventoryStockTransitQuantity = 0;
                objProductAttributesInfo.ICInventoryStockMaintainanceQuantity = 0;
                objProductAttributesInfo.ICInventoryStockRedemptionQuantity = 0;
            }
            base.Duplicate();
        }
    }
}
